////
//
/// \file   ./src/indexed_name_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/indexed_name_node.hpp>

#include <cassert>
#include <string>
#include <vector>

#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>

#include <inc/parser/internal_name_node.hpp>
#include <inc/parser/expression_node.hpp>

namespace inc {
namespace parser {

using boost::scoped_ptr;
using boost::shared_ptr;
using std::string;

/// \brief  Default constructor
/// \author Chandler Carruth
/// \date   2008.05.16
///
/// Clear out all state and variables in preparation for parsing.

IndexedNameNode::IndexedNameNode()
    : ws( new WSParser )
{}

/// \brief  Empty destructor
/// \author Chandler Carruth
/// \date   2008.05.16
///
/// Only exists here to facilitate header separation during compile.

IndexedNameNode::~IndexedNameNode() {}

/// \brief  Parse an indexed name
/// \author Chandler Carruth
/// \date   2007.07.26
///
/// This parses out an indexed name specification. The name must begin with an
/// internal name node, followed by an open bracket, one or more expressions,
/// and a close bracket.

bool
IndexedNameNode::parse( const iterator &first,
                        const iterator &last,
                        iterator &last_parsed )
{
  assert( get_text().empty() && !base_name && indices.empty()
          && "This node was parsed more than once!" );

  boost::scoped_ptr<InternalNameNode> name( new InternalNameNode );
  if( name->parse( first, last, last_parsed ) )
  {
    iterator current = last_parsed;
    base_name.swap( name );

    if( current < last && *current == '[' )
    {
      ++current;

      while( current < last )
      {
        if( ws->parse_ws( current, last, last_parsed ) )
          current = last_parsed;
        if( *current == ']' )
          break;

        boost::shared_ptr< ExpressionNode > expr( new ExpressionNode );
        if( !expr->parse( current, last, last_parsed ) )
          break;
        current = last_parsed;
        indices.push_back( expr );

        if( ws->parse_ws( current, last, last_parsed ) )
          current = last_parsed;

        if( *current != ',' || *current == ']' )
          break;
        ++current;
      }

      if( current < last && *current == ']' && !indices.empty() )
      {
        last_parsed = ++current;
        set_text( string( first, last_parsed ) );
        return true;
      }

      indices.clear();
    }

    base_name.reset();
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

} //  end parser namespace
} //  end inc namespace
